package com.linkai.controller.admin.business;


import com.linkai.controller.admin.BaseController;
import com.linkai.dto.admin.business.sale.ProductShowDto;
import com.linkai.dto.admin.business.sale.SaleOrderIndexDto;
import com.linkai.dto.admin.business.sale.SaleOrderSaveDto;
import com.linkai.entity.Saleorder;
import com.linkai.entity.SaleorderProduct;
import com.linkai.enums.ResultDTO;
import com.linkai.service.SaleorderProductService;
import com.linkai.service.SaleorderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yamon
 * @since 2021-03-08
 */
@RestController
@RequestMapping("/admin/business/sale")
public class SaleOrderController extends BaseController {
    @Autowired
    private SaleorderService saleorderService;
    @Autowired
    private SaleorderProductService saleorderProductService;

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        List<SaleOrderIndexDto> list = saleorderService.listAllSaleOrder4Index();
        modelAndView.addObject("items", list);
        modelAndView.setViewName("/admin/business/saleOrder/index");
        return modelAndView;
    }


    @GetMapping("/add")
    public ModelAndView add() {
        final ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/business/saleOrder/add");
        return modelAndView;
    }

    @PostMapping("/save")
    public ResultDTO<Boolean> saveSaleOrder(@RequestBody SaleOrderSaveDto saleOrderSaveDto) {
        //先保存主要订单
        final Saleorder entity = new Saleorder();
        BeanUtils.copyProperties(saleOrderSaveDto, entity);
        final boolean save = saleorderService.save(entity);
        //保存商品多表
        if (save) {
            final List<ProductShowDto> products = saleOrderSaveDto.getProducts();
            for (ProductShowDto product : products) {
                final SaleorderProduct saleorderProduct = new SaleorderProduct();
                //保存的是销售单的id
                saleorderProduct.setOrderId(entity.getId());
                //保存的是库存id
                saleorderProduct.setStorageId(product.getId());
                saleorderProduct.setProNum(product.getProNum());
                saleorderProduct.setProPrice(product.getProPrice());
                //相乘
                BigDecimal bg = new BigDecimal(product.getProNum());
                saleorderProduct.setEachSum(bg.multiply(product.getProPrice()));
                saleorderProduct.setCusId(entity.getCusId());
                saleorderProductService.save(saleorderProduct);
            }
            return super.successSaveInfo(true);
        } else {
            return super.successSaveInfo(false);
        }

    }

    /**
     * 查看一个销售单
     * @param id id
     * @return view
     */
    @GetMapping("/view/{id}")
    @ResponseBody
    public ModelAndView view(@PathVariable Integer id) {
        ModelAndView modelAndView = new ModelAndView();
        SaleOrderSaveDto saleOrderSaveDto = saleorderService.viewOneSaleOrderById(id);
        modelAndView.addObject("item", saleOrderSaveDto);
        modelAndView.setViewName("/admin/business/saleOrder/show");
        return modelAndView;
    }


    @GetMapping("/output/{orderId}")
    @ResponseBody
    public ResultDTO<Boolean> saveSaleOrder(@PathVariable Integer orderId) {
        //先保存主要订单
        boolean flag = saleorderService.outputStorageProductBySaleOrderId(orderId);
        return super.updateInfo(flag);
    }

}

